package com.google.code.sprite.generate;

import org.apache.commons.io.FilenameUtils;
import java.util.Map;

import com.google.code.sprite.conf.TableConfig;
import com.google.code.sprite.util.Util;

public class EntityGenerate extends  AbstractGenerate {

    public String getSubPackage() {
        return "entity";
    }

    public void writeToFile(TableConfig tableConfig, TableMetaData tableMetaData) {
        init(tableConfig, tableMetaData);
        String subDir = getJavaSubPackagePath();
        String fileName = FilenameUtils.concat(subDir, entityClass + ".java");
        writeToFile(fileName, generate());
    }


    public String generate() {
        StringBuilder sb = new StringBuilder();
        sb.append(buildPkgDeclare());
        sb.append(buildImportDeclare());
        sb.append(buildClassDeclareHead());

        Map<String, ColumnMetaData> fields = tableMetaData.getFieldsInfo();
        for (String columnName : fields.keySet()) {
            sb.append(buildFieldDeclare(fields.get(columnName)));
            sb.append("\n");
        }
        for (String columnName : fields.keySet()) {
            sb.append(buildGetter(fields.get(columnName)));
            sb.append("\n");
        }
        for (String columnName : fields.keySet()) {
            sb.append(buildSetter(fields.get(columnName)));
            sb.append("\n");
        }

        sb.append("} \n");
        return sb.toString();
    }

    public String buildPkgDeclare() {
        return "package " + this.tableConfig.getPkgName() + ".entity; \n\n";
    }

    public String buildImportDeclare() {
        return "import java.util.*; \n\n";
    }

    public String buildClassDeclareHead() {
        StringBuilder sb = new StringBuilder();
        sb.append("public class " + tableConfig.getEntityClassName() + " { \n\n");
        return sb.toString();
    }

    public String buildFieldDeclare(ColumnMetaData field) {
        return "private " + field.getJavaType()
            + " "  + field.normalizedName() + ";";
    }

    public String buildGetter(ColumnMetaData field) {
        StringBuilder sb = new StringBuilder();
        sb.append("public ");
        sb.append(field.getJavaType());
        sb.append(" ");
        sb.append("get");
        sb.append(Util.capatilize(field.normalizedName()));
        sb.append("() { \n");
        sb.append("    return this." + field.normalizedName() + " ; \n ");
        sb.append("} \n " );
        return sb.toString();
    }
    public String buildSetter(ColumnMetaData field) {
        StringBuilder sb = new StringBuilder();
        sb.append("public void ");
        sb.append("set");
        sb.append(Util.capatilize(field.normalizedName()));
        sb.append("(");
        sb.append(field.getJavaType() + " ");
        sb.append(field.normalizedName());
        sb.append(" ) { \n ");
        sb.append("  this." + field.normalizedName() + " = " + field.normalizedName() + " ; \n ");
        sb.append("} \n " );
        return sb.toString();
    }

    
}
